<!--后厨物资管理-仓库信息-添加/编辑仓库/页面-->
<template>
  <div>
    <cus-modal
      ref="modal"
      :title="form.id ? '编辑' : '添加'"
      width="400"
      @on-visible-change="onVisibleChange"
      @onOk="onOk"
    >
      <Form
        ref="formValidate"
        :model="form"
        :rules="ruleValidate"
        :label-width="100"
      >
      <FormItem
          v-if="!form.id"
          label="仓库编号:"
          prop="outerNo"
          :rules="{
            required: true,
            message: '仓库编号不能为空',
            trigger: 'blur',
          }"
        >
          <Input
            v-model="form.outerNo"
            maxlength="20"
            clearable
            @on-blur="inputItem(form.outerNo)"
            style="width: 200px"
            placeholder="请输入仓库编号"
          ></Input>
        </FormItem>
        <FormItem
          label="仓库名称:"
          prop="name"
          :rules="{
            required: true,
            message: '仓库名称不能为空',
            trigger: 'blur',
          }"
        >
          <Input
            v-model="form.name"
            maxlength="20"
            clearable
            style="width: 200px"
            placeholder="请输入仓库名称"
          ></Input>
        </FormItem>
        <FormItem
          label="责任人:"
          prop="personInCharge"
          :rules="{
            required: false,
            message: '责任人不能为空',
            trigger: 'blur',
          }"
        >
          <Input
            v-model="form.personInCharge"
            maxlength="20"
            clearable
            style="width: 200px"
            placeholder="请输入责任人"
          ></Input>
        </FormItem>
        <FormItem
          label="备注:"
          prop="remark"
          :rules="{
            required: false,
            message: '备注不能为空',
            trigger: 'blur',
          }"
        >
          <Input
            v-model="form.remark"
            maxlength="200" 
            clearable
            show-word-limit 
            type="textarea"
            :autosize="{minRows: 8}"
            :rows="8"
            style="width: 200px"
            placeholder=" 请输入备注....."
          ></Input>
        </FormItem>
      </Form>
      <Spin size="large" fix v-if="loading"></Spin>
    </cus-modal>
  </div>
</template>

<script>
import { addPostApiGoodsBasstockSave, seleteCheckOuterNoDuplicate,  updatePostApiGoodsBasstock } from "@/api/business/manager";

const form = {
  id: "",
  outerNo: "", // 仓库编号
  name: "", // 仓库名秤
  personInCharge: "", // 责任人
  remark: "", //备注
};

export default {
  components: {},
  name: "",
  data() {
    return {
      loading: false,
      form: form,
      ruleValidate: {},

    };
  },
  mounted() {
    this.form = this.$cu.deepClone(form);
  },
  methods: {
    onVisibleChange(v) {
      this.$refs["formValidate"].resetFields();
      if (!v) this.form = this.$cu.deepClone(form);
    },
    show() {
      this.loading = false;
      this.$refs["modal"].show = true;
    },
    onOk() {
      this.$refs["formValidate"].validate((valid) => {
        if (valid) {
            if(this.form.id) {
                console.log('edit')
                this.updatePostApiGoodsBasstockApi();
            } else {
                console.log('add')
                this.addPostApiGoodsBasstockSaveApi();
            }
        }
      });
    },
    inputItem(v) {
      this.seleteCheckOuterNoDuplicateApi(v)
    },
    // 保存前仓库编号重复性检查-------------接口
    seleteCheckOuterNoDuplicateApi(outerNo) {
      return new Promise((resolve) => {
        seleteCheckOuterNoDuplicate({outerNo: outerNo}).then((res) => {
          if (res.retCode == 2000) {
            console.log(res)
            if(res.data){
              this.$Message.info('仓库编号已存在');
            }
            resolve();
          } 
        });
      });
    },
    //保存仓库-----------------------接口
    addPostApiGoodsBasstockSaveApi() {
      return new Promise((resolve) => {
        let params = this.form;
        addPostApiGoodsBasstockSave(params).then((res) => {
          if (res.retCode == 2000) {
            this.$Message.success(res.retMsg);
            this.$refs["modal"].show = false;
            this.$parent.getBasstockSelectListApi();
            resolve();
          } 
        });
      });
    },
    //编辑仓库---------------------接口
    updatePostApiGoodsBasstockApi() {
        return new Promise((resolve) => {
        let params = this.form;
        updatePostApiGoodsBasstock(params).then((res) => {
          if (res.retCode == 2000) {
            this.$Message.success(res.retMsg);
            this.$refs["modal"].show = false;
            this.$parent.getBasstockSelectListApi();
            resolve();
          } 
        });
      });
    }
  },
};
</script>

<style scoped lang="less">
</style>
